﻿using MCPlatform.MotionLib.Base.AssitantClass;
using MCPlatform.MotionLib.Base.Environment;
using MCPlatform.UI.Controls.Models;
using Panuon.WPF.UI;
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;

namespace MCPlatform.UI.Controls.Views
{
    /// <summary>
    /// PointSetting.xaml 的交互逻辑
    /// </summary>
    public partial class PointSetting : UserControl
    {
        public PointSetting()
        {
            InitializeComponent();
        }


        public PointSettingModel PointSettingModel
        {
            get { return (PointSettingModel)GetValue(PointSettingModelProperty); }
            set { SetValue(PointSettingModelProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PointSettingModel.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PointSettingModelProperty =
            DependencyProperty.Register("PointSettingModel", typeof(PointSettingModel), typeof(PointSetting), new PropertyMetadata(default));

        private void btn_getActualPos_Click(object sender, RoutedEventArgs e)
        {
            if (PointSettingModel.MotionEntity is null || PointSettingModel is null)
            {
                return;
            }
              
            this.PointSettingModel.Parameter = PointSettingModel.MotionEntity.ActualPosition;
            Toast.Show("获取成功！", targetWindow: ToastWindow.MainWindow);
        }

        private void btn_MoveToPos_Click(object sender, RoutedEventArgs e)
        {
            if (PointSettingModel.MotionEntity is null || PointSettingModel is null)
            {
                return;
            }

            var res = MessageBoxX.Show( "请确认当前移动轴无干涉再移动", "警告", MessageBoxButton.YesNo, MessageBoxIcon.Warning);

            if (res == MessageBoxResult.No)
            {
                return;
            }

            bool parseResult = double.TryParse(this.tb_point.Text,out double pos);
            if (!parseResult) 
            {
                Toast.Show("位置解析错误！",targetWindow: ToastWindow.MainWindow);
                return;
            }

            PointSettingModel.MotionEntity.AbsMove(pos);
        }

        private void btn_apply_Click(object sender, RoutedEventArgs e)
        {
            bool parseResult = double.TryParse(this.tb_point.Text, out double pos);
            if (!parseResult)
            {
                Toast.Show("位置解析错误！", targetWindow: ToastWindow.MainWindow);
                return;
            }
          

            String path = Paths.PointConfigPaths;

            SaveConfigData.SaveConfig<PointSettingModel>(path, this.PointSettingModel);

            

        }

        private void tb_point_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
        {

            e.Handled = !IsTextAllowed(e.Text, sender);


            if (!PointSettingModel.IsPosition)
            {
                e.Handled = false;
                return;
            }

             

            bool res = double.TryParse(this.tb_point.Text, out double pos);

            if (!res)
            {
                e.Handled = false;
                return;
            }

            if (pos >= PointSettingModel?.MotionEntity?.AxisConfig.SPLimit)
            {
                e.Handled = true;
                Toast.Show("位置超出当前轴正限位", targetWindow: ToastWindow.MainWindow);


            }
            else if ((pos <= PointSettingModel?.MotionEntity?.AxisConfig.SNLimit))
            {
                e.Handled = true;
                Toast.Show("位置超出当前轴负限位", targetWindow: ToastWindow.MainWindow);
            }

            

        }

        private bool IsTextAllowed(string text,object sender)
        {
            // 允许数字、小数点（但不能有两个连续的小数点）和负号（只能在开头）  
            if (text == "-" && ((TextBox)sender).Text.Length == 0) return true; // 负号只允许在开头  
            if (text == ".")
            {
                string textBoxText = ((TextBox)sender).Text;
                return !textBoxText.Contains("."); // 确保没有已存在的小数点  
            }
            return char.IsDigit(text, 0); // 只允许数字  
        }


    }
}
